//
//  LoginViewController.m
//  ShopProject
//
//  Created by 小焘无量 on 2018/9/27.
//  Copyright © 2018年 ShopProject. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self UIConfig];
    
    [self dataTest];
}

- (void)UIConfig {
    _loginBtn.backgroundColor = MainColor;
    self.title = @"登录";
}

/// 登录
- (IBAction)loginUpBtnPress:(id)sender {
    
    [self.view endEditing:YES];
    
    if (![self loginDateCheck]) {
        return;
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary new];
    dic[@"userName"] = _userNameTF.text;
    dic[@"passWord"] = _passwordTF.text;
    
    
    JQFMDB *dataBase = [JQFMDB shareDatabase];
    NSArray *arr = [dataBase jq_lookupTable:USERINFOTABLE dicOrModel:[OO_UserInfoModel class] whereFormat:@"where phone = %@",_userNameTF.text];
    OO_UserInfoModel *model = arr.firstObject;
    if (!model || ![model.password isEqualToString:_passwordTF.text]) {
        // 账号密码错误
        dic[@"type"] = @"0";
        
        [PPNetworkHelper POST:[NSString stringWithFormat:@"%@%@",HTTP,LOGIN_URL] parameters:dic success:^(id responseObject) {
            NSDictionary *dict = (NSDictionary *)responseObject;
            NSLog(@"%@",dict);
            [MBProgressHUD showTipMessageInView:@"账号密码不正确"];
        } failure:^(NSError *error) {
            [MBProgressHUD showTipMessageInView:@"网络请求失败"];
        }];
    }else {
        // 登录成功
        dic[@"type"] = @"1";
        NSString *telStr = _userNameTF.text;
        [PPNetworkHelper POST:[NSString stringWithFormat:@"%@%@",HTTP,LOGIN_URL] parameters:dic success:^(id responseObject) {
            NSDictionary *dict = (NSDictionary *)responseObject;
            NSLog(@"%@",dict);
            
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            UITabBarController *tab = [storyboard instantiateViewControllerWithIdentifier:TABBAR_CONTROLLER];
            UIWindow *window = [UIApplication sharedApplication].keyWindow;
            window.rootViewController = tab;
            
            [UserDefaults writeToPlist:telStr withKey:LOGIN_STATUS_TEL];
            [MBProgressHUD showTipMessageInView:@"登录成功"];
        } failure:^(NSError *error) {
            [MBProgressHUD showTipMessageInView:@"网络请求失败"];
        }];
    }
}


/// 立即注册
- (IBAction)registerBtnPress:(id)sender {
    RegisterViewController *vc = [RegisterViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

/// 忘记密码
- (IBAction)forgetPasswodBtnPress:(id)sender {
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",Customer_Service];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}

// 数据检测
-(BOOL)loginDateCheck {
    if ([CommonAPI StringIsNull:_userNameTF.text]) {
        [MBProgressHUD showTipMessageInView:@"请输入用户名"];
        return NO;
    }
    if (![CommonAPI telPhoneCheck:_userNameTF.text]) {
        [MBProgressHUD showTipMessageInView:@"请输入正确的用户名"];
        return NO;
    }
    if ([CommonAPI StringIsNull:_passwordTF.text]) {
        [MBProgressHUD showTipMessageInView:@"请输入密码"];
        return NO;
    }
    return YES;
}

// 单元测试
-(void)dataTest {
    _userNameTF.text = @"13219011308";
    _passwordTF.text = @"123456";
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
